<template>
  <div :class="['hm-input', { 'hm-input--suffix': showSuffix }]">
    <input :type="showPassword ? (passwordVisiable ? 'text' : 'password') : type"
           :placeholder="placeholder"
           :name="name"
           :value="value"
           :disabled="disabled"
           :class="[
        'hm-input__inner',
        {
          'is-disabled': disabled
        }
      ]"
           @input="handleInput"
    />
    <span v-if="showSuffix" class="hm-input__suffix">
      <!-- 清空按钮 -->
      <i v-if="clearable && value" class="hm-input__icon hm-icon-circle-close" @click="clear"></i>
      <!-- 显示密码 -->
      <i
        v-if="showPassword"
        :class="[
          'hm-input__icon',
          'hm-icon-view',
          {
            'hm-icon-view-active': passwordVisiable
          }
        ]"
        @click="handlePassword"
      ></i>
    </span>
  </div>
</template>

<script>
export default {
  name: 'HmInput',
  props: {
    placeholder: {
      type: String,
      default: ''
    },
    type: {
      // input 类型：text | password
      type: String,
      default: 'text'
    },
    name: {
      type: String,
      default: ''
    },
    disabled: {
      // 禁用
      type: Boolean,
      default: false
    },
    value: {
      type: String,
      default: ''
    },
    clearable: {
      // 可清空
      type: Boolean,
      default: false
    },
    showPassword: {
      // 显示密码
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 用于控制是否显示密码框
      passwordVisiable: false
    }
  },
  computed: {
    showSuffix () {
      return this.clearable || this.showPassword
    }
  },
  methods: {
    handleInput (e) {
      this.$emit('input', e.target.value)
    },
    clear () {
    // 把内容清空
      this.$emit('input', '')
    },
    handlePassword () {
      this.passwordVisiable = !this.passwordVisiable
    }
  }
}
</script>

<style lang="scss">
  .hm-input {
    width: 180px;
    position: relative;
    font-size: 14px;
    display: inline-block;

    .hm-input__inner {
      cursor: pointer;
      -webkit-appearance: none;
      background-color: #fff;
      background-image: none;
      border-radius: 4px;
      border: 1px solid #dcdfe6;
      box-sizing: border-box;
      color: #000;
      display: inline-block;
      font-size: inherit;
      height: 40px;
      line-height: 40px;
      outline: none;
      padding: 0 15px;
      transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
      width: 100%;

      &:focus {
        outline: none;
        border-color: #409eff;
      }

      &.is-disabled {
        background-color: #f5f7fa;
        border-color: #e4e7ed;
        color: #c0c4cc;
        cursor: not-allowed;
      }
    }
  }

  .hm-input--suffix {
    .hm-input__inner {
      padding-right: 30px;
    }

    .hm-input__suffix {
      position: absolute;
      height: 100%;
      right: 10px;
      top: 0;
      line-height: 40px;
      text-align: center;
      color: #c0c4cc;
      transition: all 0.3s;
      z-index: 900;

      i {
        color: #c0c4cc;
        font-size: 14px;
        cursor: pointer;
        transition: color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
      }
    }
  }

  .hm-input--suffix {
    .hm-input__inner {
      padding-right: 30px;
    }

    .hm-input__suffix {
      position: absolute;
      height: 100%;
      right: 10px;
      top: 0;
      line-height: 40px;
      text-align: center;
      color: #c0c4cc;
      transition: all 0.3s;
      z-index: 900;

      i {
        color: #c0c4cc;
        font-size: 14px;
        cursor: pointer;
        transition: color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);

        &.hm-icon-view-active {
          color: blue;
        }
      }
    }
  }
</style>
